package com.blog.service;

import com.blog.mapper.FadadaSignMapper;
import com.blog.mapper.SecBusinessMapper;
import com.blog.properties.FadadaProperties;
import com.blog.seckill.business.FadadaSign;
import com.blog.seckill.business.SecBusiness;
import com.blog.util.fadada.FaDaDaInit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName fadSystemService
 * @Destription
 * @Author 天堂小野花
 * @Date 2025/7/14  18:19
 * @Version 1.0
 */
@Service
@Slf4j
public class FadSystemService {
    @Autowired
    private FadadaProperties fadadaProperties;
    @Autowired
    FadadaSignMapper fadadaSignMapper;
    @Autowired
    SecBusinessMapper secBusinessMapper;

    public Map<String, Object>  createOrder(SecBusiness secBusiness) throws Exception {
        FaDaDaInit faDaDaInit = new FaDaDaInit();
        FadadaSign fadadaSign = new FadadaSign();
        String token = faDaDaInit.createApiClient(fadadaProperties);
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String dateStr = simpleDateFormat.format(date);
        System.out.println(token);
        // 一、创建签署任务
        // 1.构造请求参数
        Map<String, Object> bizContent = new HashMap<>();
        bizContent.put("signTaskSubject", "签署任务"+dateStr);
        bizContent.put("signTemplateId", fadadaProperties.getSignTemplateId());
        bizContent.put("autoStart", true);
        bizContent.put("transReferenceId",dateStr);

        Map<String, Object> initiator = new HashMap<>();
        initiator.put("idType", "corp");
        initiator.put("openId", fadadaProperties.getOpenCorpId());
        bizContent.put("initiator", initiator);

        List<Map<String, Object>> actors = new ArrayList<>();
        Map<String, Object> actorCrop = new HashMap<>();
        Map<String, Object> actorCropDetail = new HashMap<>();
        actorCropDetail.put("actorId", "甲方");
        actorCropDetail.put("actorType", "corp");
        actorCropDetail.put("actorName", fadadaProperties.getCorpName());
        actorCropDetail.put("permissions", Arrays.asList("fill", "sign"));
        actorCropDetail.put("sendNotification", "false");
        actorCrop.put("actor", actorCropDetail);

        Map<String, Object> signCropConfigInfo = new HashMap<>();
        signCropConfigInfo.put("blockHere", false);
        signCropConfigInfo.put("requestVerifyFree", false);
        actorCrop.put("signConfigInfo", signCropConfigInfo);

        Map<String, Object> actor = new HashMap<>();
        Map<String, Object> actorDetail = new HashMap<>();
        actorDetail.put("actorId", "乙方");
        actorDetail.put("actorType", "person");
        actorDetail.put("actorName", secBusiness.getPhone());
        actorDetail.put("permissions", Arrays.asList("fill", "sign"));
        actorDetail.put("sendNotification", "true");
        actorDetail.put("notifyType", "start");
        actorDetail.put("notifyAddress", secBusiness.getEmail());
        actor.put("actor", actorDetail);

        Map<String, Object> signConfigInfo = new HashMap<>();
        signConfigInfo.put("blockHere", false);
        signConfigInfo.put("requestVerifyFree", false);
        actor.put("signConfigInfo", signConfigInfo);

        actors.add(actor);
        actors.add(actorCrop);
        bizContent.put("actors", actors);

        // 2.调用方法
        String taskId = faDaDaInit.createFasc(fadadaProperties,token, bizContent);
        log.info("bizContent:{}", bizContent);
        log.info("taskId:{}", taskId);
        // 二、获取参与方签署链接
        Map<String, Object> signUrlsBizContent = new HashMap<>();
        signUrlsBizContent.put("signTaskId", taskId);
        signUrlsBizContent.put("actorId", "乙方");
        Map<String, Object> signUrls = faDaDaInit.getSignUrl(fadadaProperties,token, signUrlsBizContent);
        log.info("signUrlsBizContent:{}", signUrlsBizContent);
        log.info("signUrls:{}", signUrls);
        //将签署结果存放到数据库中
        fadadaSign.setSignTaskId(taskId);
        fadadaSign.setActorSignTaskUrl(signUrls.get("actorSignTaskUrl").toString());
        fadadaSign.setActorSignTaskEmbedUrl(signUrls.get("actorSignTaskEmbedUrl").toString());
        fadadaSign.setSignTaskSubject("签署任务"+dateStr);
        fadadaSign.setTransReferenceId(secBusiness.getPhone());
        fadadaSign.setNotifyAddress(secBusiness.getEmail());
        fadadaSign.setState(0);
        fadadaSign.setParentId(secBusiness.getId());
        fadadaSignMapper.insertSelective(fadadaSign);
        secBusiness.setState(2);
        secBusinessMapper.updateByPrimaryKey(secBusiness);
        // 三、将签署链接返回给前端
        Map<String, Object> result = new HashMap<>();
        result.put("taskId", taskId);
        result.putAll(signUrls);
        return result;
    }

    public FadadaSign fadByParentId(Integer rid) {
        return fadadaSignMapper.fadByParentId(rid);
    }

    public String orderCallback(HttpHeaders headers, String bizContent) throws Exception {
        FaDaDaInit faDaDaInit = new FaDaDaInit();
        String result = faDaDaInit.fddEventCallback(fadadaProperties,headers,bizContent);
        return result;
    }

    public int fadCallback(SecBusiness secBusiness) {
        secBusiness.setState(3);
        return secBusinessMapper.updateByPrimaryKeySelective(secBusiness);
    }
}
